package com.blockchain.front.servlets.api.user;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.service.ServiceSession;
import com.blockchain.front.service.financial.FinancialManage;
import com.blockchain.front.servlets.api.AbstractTickerServlet;
import com.blockchain.front.servlets.api.ErrorCode;
import com.blockchain.front.servlets.api.ticker.ResultMessage;
import com.blockchain.front.util.JsonUtil;
import com.blockchain.util.StringHelper;

/**
 * 机器人API接口</br>
 * 撤销订单</br>
 * 修改时间：2017年7月24日16:01:35</br>
 */
@WebServlet(value = { "/api/user/cancelOrder" })
public class CancelOrder extends AbstractTickerServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		ResultMessage msg = new ResultMessage();
		int id = 0;
		String apikey = "";
		String[] data = new String[2];
		data[0] = "0";
		data[1] = "撤销失败";
		try {
			id = Integer.parseInt(request.getParameter("id"));
			apikey = request.getParameter("apikey");
		} catch (Exception e) {
			msg.error = ErrorCode.CSCU.getErrorCode();
			msg.msg = ErrorCode.CSCU.getName();

		}
//		System.out.printf("撤销订单-请求参数,%d,%s\n", id, apikey);
		if (id > 0 && !StringHelper.isEmpty(apikey)) {
			FinancialManage manage = serviceSession.getService(FinancialManage.class);
			try {
				manage.cancelOrder(id, apikey);
				data[0] = "1";
				data[1] = "撤销成功";
			} catch (Exception e) {
				msg.error = ErrorCode.CXCU.getErrorCode();
				msg.msg = e.getMessage();
			}
		} else {
			msg.error = ErrorCode.CXCU.getErrorCode();
			msg.msg = ErrorCode.CXCU.getName();
		}
		msg.data = data;
		String jsonData = JsonUtil.GsonString(msg);
//		System.out.printf("撤销订单,%s\n", jsonData);
		response.getWriter().write(jsonData);
	}

	@Override
	protected void processGet(HttpServletRequest request, HttpServletResponse response, ServiceSession serviceSession)
			throws Throwable {
		processPost(request, response, serviceSession);
	}
}
